/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.helper;

import android.content.Context;
import android.content.Intent;
import android.graphics.*;
import android.os.Handler;
import android.view.*;
import android.widget.*;
import com.feiyue.basic.server.cache.CacheFetch;
import com.feiyue.basic.server.cache.DiskLruCache;
import com.feiyue.basic.server.dl.DlService;
import com.feiyue.basic.server.parser.Book;
import java.io.File;
import java.util.List;

public class BookShelfAdapter extends SimpleAdapter
{
	public class MyThread extends Thread
	{

		private Context context;
		private String coverAddress;
		private Handler handler;
		private Myrunnable myrunnable;

		public void run()
		{
			File file = CacheFetch.getFile(context, coverAddress, coverAddress, false);
			if (file != null) {
				try {
					Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
					
					if (bitmap != null)
					{
						int i = bitmap.getWidth();
						int j = bitmap.getHeight();
						float f = (float)(width / (double)i);
						float f1 = (float)(height / (double)j);
						Matrix matrix = new Matrix();
						matrix.postScale(f, f1);
						Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, i, j, matrix, true);
						myrunnable.setBitmap(bitmap1);
						handler.post(myrunnable);
					}
				} catch (OutOfMemoryError e) {
					System.gc();
				}
			}
		}

		public MyThread(Context context1, Myrunnable myrunnable1, Handler handler1, String s)
		{
			super();
			context = context1;
			myrunnable = myrunnable1;
			handler = handler1;
			coverAddress = s;
		}
	}

	public class Myrunnable
		implements Runnable
	{

		private Bitmap bitmap;
		private ImageView coverImageView;

		public void run()
		{
			coverImageView.setImageBitmap(bitmap);
		}

		public void setBitmap(Bitmap bitmap1)
		{
			bitmap = bitmap1;
		}

		public Myrunnable(ImageView imageview)
		{
			super();
			coverImageView = null;
			bitmap = null;
			coverImageView = imageview;
		}
	}


	private List booklist;
	private List data;
	private double height;
	private LayoutInflater inflater;
	private Context mContext;
	private int resource;
	private double width;

	public BookShelfAdapter(Context context, List list, int i, String as[], int ai[], List list1)
	{
		super(context, list, i, as, ai);
		mContext = context;
		inflater = (LayoutInflater)context.getSystemService("layout_inflater");
		data = list;
		resource = i;
		booklist = list1;
		width = BitmapFactory.decodeResource(mContext.getResources(), 0x7f0200b1).getWidth();
		height = BitmapFactory.decodeResource(mContext.getResources(), 0x7f0200b1).getHeight();
	}

	public View getView(final int position, View view, ViewGroup viewgroup)
	{
		ImageView imageview1;
		Bitmap bitmap;
		if (view == null)
			view = inflater.inflate(resource, viewgroup, false);
		((LinearLayout)view.findViewById(0x7f06001b)).setVisibility(0);
		((ImageView)view.findViewById(0x7f060032)).setVisibility(8);
		ImageView imageview = (ImageView)view.findViewById(0x7f06001a);
		File file;
		DiskLruCache disklrucache;
		File file1;
		Bitmap bitmap1;
		if (((Book)booklist.get(position)).getdlstate() == 0)
		{
			imageview.setImageResource(0x7f020026);
			imageview.setVisibility(0);
			android.view.View.OnClickListener onclicklistener = new android.view.View.OnClickListener() {

				public void onClick(View view1)
				{
					Intent intent = new Intent(mContext, DlService.class);
					intent.putExtra("categoryid", ((Book)booklist.get(position)).getCategoryId());
					intent.putExtra("bookid", ((Book)booklist.get(position)).getId());
					intent.putExtra("bookname", ((Book)booklist.get(position)).getName());
					intent.putExtra("issuestate", ((Book)booklist.get(position)).getIssuestate());
					mContext.startService(intent);
				}
			};
			imageview.setOnClickListener(onclicklistener);
		} else
		{
			imageview.setImageResource(0x7f020027);
			imageview.setVisibility(0);
		}
		((TextView)view.findViewById(0x7f06002f)).setText(((Book)booklist.get(position)).getName());
		((TextView)view.findViewById(0x7f060030)).setText((new StringBuilder("作者：")).append(((Book)booklist.get(position)).getAuthor()).toString());
		((TextView)view.findViewById(0x7f060031)).setText((new StringBuilder("状态：")).append(((Book)booklist.get(position)).getIssuestate()).toString());
		imageview1 = (ImageView)view.findViewById(0x7f06002d);
		imageview1.setImageResource(((Book)booklist.get(position)).getCoverid(Boolean.valueOf(true)));
		if (((Book)booklist.get(position)).getCoverAddr() != null && !((Book)booklist.get(position)).getCoverAddr().equals("null")) {
			file = DiskLruCache.getDiskCacheDir(mContext, "http");
			disklrucache = DiskLruCache.openCache_get(mContext, file, 0xa00000L);
			file1 = new File(disklrucache.createFilePath(((Book)booklist.get(position)).getCoverAddr()));
			if (!disklrucache.containsKey(((Book)booklist.get(position)).getCoverAddr())) {
				Myrunnable myrunnable = new Myrunnable(imageview1);
				Handler handler = new Handler();
				(new MyThread(mContext, myrunnable, handler, ((Book)booklist.get(position)).getCoverAddr())).start();
			} else {
				try {
					bitmap = BitmapFactory.decodeFile(file1.getPath());
					
					if (bitmap != null)
					{
						int i = bitmap.getWidth();
						int j = bitmap.getHeight();
						float f = (float)(width / (double)i);
						float f1 = (float)(height / (double)j);
						Matrix matrix = new Matrix();
						matrix.postScale(f, f1);
						imageview1.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, i, j, matrix, true));
					}
				} catch (OutOfMemoryError e) {
					System.gc();
				}
			}
		}
		
		return view;
	}
}